package com.topboya.linker.ui.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.response.ProductInforListReponse;
import com.topboya.linker.ui.activity.SeeProductInforActivity;
import com.topboya.linker.ui.activity.xSeeFileActivity;

import java.util.ArrayList;

public class SeeProductAdapter extends RecyclerView.Adapter<SeeProductAdapter.ListItemViewHolder> {

    private static final String TAG = "SelectProductAdapter:";
    private SeeProductInforActivity mContext;
    private ArrayList<ProductInforListReponse.ProductDetailInfoList> mList;
//   用来保存被选中的对象


    public SeeProductAdapter(SeeProductInforActivity mContext, ArrayList<ProductInforListReponse.ProductDetailInfoList> list) {

        if (list == null) {

            throw new IllegalArgumentException("model Data must not be null");
        }
        this.mContext = mContext;
        mList = list;
    }



    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.select_product_item, viewGroup, false);
        return new SeeProductAdapter.ListItemViewHolder(itemView);
    }






    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final ListItemViewHolder holder, final int i) {

        //设置条目状态
        holder.tv_productName.setText(mList.get(i).getProductName());
        int isvisible =Integer.parseInt(mList.get(i).getFileNum()) > 0 ? View.VISIBLE : View.GONE;
        holder.img_data.setVisibility(isvisible);
        holder.tv_productcompany.setText(mList.get(i).getProductFactory());
        if (null !=mList.get(i).getFileLists() && mList.get(i).getFileLists().size() > 0){

            holder.tv_select_file_num.setVisibility(View.VISIBLE);
            holder.tv_select_file_num.setText(String.valueOf(mList.get(i).getFileLists().size()));

        }else {

            holder.tv_select_file_num.setVisibility(View.INVISIBLE);
        }

        //条目view的监听
        holder.itemView.setOnClickListener(v -> {

            if (null !=itemClickListener){
                itemClickListener.onClick(holder.itemView ,i);
            }
        });
        holder.img_data.setOnClickListener(v1 -> {

            if (null !=itemClickListener){

                itemClickListener.onClick(holder.itemView ,i);
            }
//            ArrayList<ProductInforListReponse.ProductDetailInfoList> productDetailInfoLists = getSelectedItem();
            Intent intent = new Intent(mContext,xSeeFileActivity.class);
//            将点点击的位置传过去，回来如果需要更新数据直接更新数据，不用遍历
            intent.putExtra("productId",mList.get(i).getId());
            mContext.startActivityForResult(intent,1000);

        });

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        TextView tv_productName;
        TextView tv_productcompany;
        ImageView img_data;
        TextView tv_select_file_num;

        ListItemViewHolder(View view) {
            super(view);
            this.tv_productName = view.findViewById(R.id.tv_productName);
            this.tv_productcompany = view.findViewById(R.id.tv_productcompany);
            this.img_data = view.findViewById(R.id.img_data);
            this.tv_select_file_num = view.findViewById(R.id.tv_select_file_num);

        }

    }

    private SeeProductAdapter.OnItemClickListener itemClickListener;

    public void setOnItemClickListener(SeeProductAdapter.OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }

    public interface  OnItemClickListener{

        void onClick(View v, int position);
    }



}
